﻿Public Class GridViewGroupHelper
  Public Shared Sub GroupGridView(gvrc As GridViewRowCollection, startIndex As Integer, total As Integer)
    If total = 0 Then
      Return
    End If
    Dim i As Integer, count As Integer = 1
    Dim lst As New ArrayList()
    lst.Add(gvrc(0))
    Dim ctrl = gvrc(0).Cells(startIndex)
    For i = 1 To gvrc.Count - 1
      Dim nextCell As TableCell = gvrc(i).Cells(startIndex)
      If ctrl.Text = nextCell.Text Then
        count += 1
        nextCell.Visible = False
        lst.Add(gvrc(i))
      Else
        If count > 1 Then
          ctrl.RowSpan = count
          GroupGridView(New GridViewRowCollection(lst), startIndex + 1, total - 1)
        End If
        count = 1
        lst.Clear()
        ctrl = gvrc(i).Cells(startIndex)
        lst.Add(gvrc(i))
      End If
    Next
    If count > 1 Then
      ctrl.RowSpan = count
      GroupGridView(New GridViewRowCollection(lst), startIndex + 1, total - 1)
    End If
    count = 1
    lst.Clear()
  End Sub

End Class
